﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using PhanMemDoVui.BusinessLogic;
using PhanMemDoVui.BusinessObject;

namespace PhanMemDoVui
{
    public partial class frmAddQuestion : Form
    {
        public frmAddQuestion()
        {
            InitializeComponent();
            this.Icon = Properties.Resources.Lamp;
        }

        private void label2_Click(object sender, EventArgs e)
        {

        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void textBox3_TextChanged(object sender, EventArgs e)
        {

        }

        private void frmAddQuestion_Load(object sender, EventArgs e)
        {

        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }

        private void btn_create_Click(object sender, EventArgs e)
        {
            QuestionsBUS question = new QuestionsBUS();
            QuestnAns qa = new QuestnAns();
            UserBUS user = UserBUS.Instance();
            if (txt_question.Text == "" || txt_ans1.Text == "" || txt_ans2.Text == "" || txt_ans3.Text == "" || txt_ans4.Text == "" || cmb_list.Text == "" || (rad_easy.Checked == false && rad_mode.Checked == false && rad_hard.Checked == false && rad_vhar.Checked == false))
            {
                lab_success.Hide();
                lab_wrong.Show();
            }
            else if (txt_question.Text.Length > 100 || txt_ans1.Text.Length > 50 || txt_ans2.Text.Length > 50 || txt_ans3.Text.Length > 50 || txt_ans4.Text.Length > 50)
                MessageBox.Show("Câu hỏi hoặc đáp án quá dài.", "QuizShow - Lỗi");
            else
            {
                string diff = null;
                bool success;
                if (rad_easy.Checked)
                    diff = "easy";
                else if (rad_mode.Checked)
                    diff = "mode";
                else if (rad_hard.Checked)
                    diff = "hard";
                else if (rad_vhar.Checked)
                    diff = "vhar";
                System.Text.Encoding ascii = System.Text.Encoding.ASCII;
                qa.Question = txt_question.Text;
                qa.Ans1 = txt_ans1.Text;
                qa.Ans2 = txt_ans2.Text;
                qa.Ans3 = txt_ans3.Text;
                qa.Ans4 = txt_ans4.Text;
                qa.RAns = System.Convert.ToInt32(cmb_list.Text[0]) - 64;
                qa.Difficulty = diff;
                qa.CreatedBy = user.GetUsername();

                lab_wrong.Hide();
                success = question.ThemCauHoi(qa);
                if (success == false)
                    MessageBox.Show("Thêm thất bại, câu hỏi này đã được tạo", "Thêm thất bại", MessageBoxButtons.OK, MessageBoxIcon.Error);
                else
                    lab_success.Show();
                txt_question.Clear();
                txt_ans1.Clear();
                txt_ans2.Clear();
                txt_ans3.Clear();
                txt_ans4.Clear();
            }
        }

        private void btn_back_Click(object sender, EventArgs e)
        {
            UserBUS user = UserBUS.Instance();
            if (user.IsAdmin())
            {
                frmAdmin admin = new frmAdmin();
                this.Hide();
                admin.Show();
            } 
            else
            {
                frmCommonUser comm = new frmCommonUser();
                this.Hide();
                comm.Show();
            }
        }

        private void frmAddQuestion_FormClosed(object sender, FormClosedEventArgs e)
        {
            Application.Exit();
        }

    }
}
